//
//  Location.swift
//  Location
//
//  Created by cygarm on 15/12/10.
//  Copyright © 2015年 cygarm. All rights reserved.
//

import Foundation
import CoreData
import MapKit

/*
crash: @objc会导致找不到class Locationx
*/
//@objc(Location)
class Location: NSManagedObject, MKAnnotation {
	var coordinate: CLLocationCoordinate2D {
		return CLLocationCoordinate2DMake(latitude, longitude)
	}
	
	var title: String? {
		if locationDescription.isEmpty {
			return "(No Description)"
		} else {
			return locationDescription
		}
	}
	
	var subtitle: String? {
		return category
	}
	
	var hasPhoto: Bool {
		return photoID != nil
	}
	
	var photoPath: String {
		assert(photoID != nil, "No photo ID set")
		let filename = "Photo-\(photoID!.integerValue).jpg"
				
		return NSURL(fileURLWithPath: applicationDocumentsDirectory).URLByAppendingPathComponent(filename).path!
	}
	
	var photoImage: UIImage? {
		return UIImage(contentsOfFile: photoPath)
	}
	
	class func nextPhotoID() -> Int {
		let userDefaults = NSUserDefaults.standardUserDefaults()
		let currentID = userDefaults.integerForKey("PhotoID")
		userDefaults.setInteger(currentID + 1, forKey: "PhotoID")
		userDefaults.synchronize()
		return currentID
	}
	
	func removePhotoFile() {
		if hasPhoto {
			let path = photoPath
			let fileManager = NSFileManager.defaultManager()
			if fileManager.fileExistsAtPath(path) {
				do {
					try fileManager.removeItemAtPath(path)
				} catch {
					print("Error removing file: \(error)")
				}
			}
		}
	}
}
